<?php
namespace app\souther\controller;

use app\common\model\Link as LinkModel;
use think\Db;

class Link extends Base
{
	
	function __construct()
	{
		parent::_initialize();
	}


	/**
     * [index  列表]
     * @author [南風]
     * @return mixed|string
     * @throws \Exception
     */
    public function index()
    {
        $model = new LinkModel();

        if(request()->isAjax())
        {
            $data = input('param.');
            $map = [];

            if(is_array($data)){
                if(!empty($data['key']))
                    $map['name'] = ['like',"%" . $data['key'] . "%"];
            }
            $this->getPageAndSize($data);

            try{
                $count =$model->getLinkCountByCondition($map);
                $allpage = intval(ceil($count / $this->size));//总页面
                $lists = $model->getLinkByCondition($map, $this->from, $this->size);//列表
            }catch (\Exception $e)
            {
                writelog($e->getMessage(),-1);
                exception($e->getMessage());
            }

            if(empty($lists))
                return '';
            
            writelog('查看友情链接列表');

            return $this->fetch('ajax_list',[
                'count' => $count,//总记录数
                'allpage' => $allpage,
                'lists' => $lists,
            ]);
        }

        return $this->fetch('',['app'=>'link']);
    }

    /**
     * [add  添加]
     * @author [南風]
     * @return array|mixed
     */
    public function add()
    {
        $model = new LinkModel();

        if(request()->isPost()){
            try{
                $param = input('post.');
                $id = $model->addData($param);
            }catch(\Exception $e){
                writelog($e->getMessage(),-1);
                return admin_json(-1,$model->getError());
            }
            writelog('添加友情链接--'.$id);
            return admin_json(1,'成功','',url('/link'));
        }

        return $this->fetch('add');
    }

    /**
     * [edit  编辑]
     * @author [南風]
     * @return array|mixed
     */
    public function edit()
    {
        $model = new LinkModel();

        if(request()->isPost()){
            try{
                $param = input('post.');
                $model->editData($param);
            }catch(\Exception $e){
                writelog($e->getMessage(),-1);
                return admin_json(-1,$model->getError());
            }
            writelog('修改友情链接--'.$param['id']);
            return admin_json(1,'成功','',url('/link'));
        }

        return $this->fetch('edit',[
            'edit' => $model->findData(input('param.id'))
        ]);
    }

    /**
     * [del  删除]
     * @author [南風]
     * @return array
     */
    public function del()
    {
        $model = new LinkModel();
        $adModel = new Ad();

        $id = input('param.id');

        $model->deleteTrueData($id);

        writelog('删除友情链接--'.$id);

        return admin_json(1,'成功');
    }

    /**
     * [status 状态]
     * @author [南風]
     * @return array
     */
    public function status()
    {
        $model = new LinkModel();
        $data=input('param.');
        $res = $model->changeStatus(['id' => $data['id']],$data['status']);

        if($res)
        {
            if($data['status'] == 0)
            {
                writelog('禁用友情链接状态--'.$data['id']);
                $statusurl = url('status',['id'=>$data['id'],'status'=>1]);
                return admin_json(2,'已禁止',['statusurl'=>$statusurl]);
            }
            else
            {
                writelog('启用友情链接状态--'.$data['id']);
                $statusurl = url('status',['id'=>$data['id'],'status'=>0]);
                return admin_json(1,'已启用',['statusurl'=>$statusurl]);
            }
        }
        return admin_json(-1,'操作失败');
    }
}